var express = require('express');
var router = express.Router();
const multiparty = require('multiparty')
const { BusinessModel } = require('../mongodb/db')
const jwt = require('jsonwebtoken')
const xlsx = require('node-xlsx')
const AlipaySdk = require('alipay-sdk').default;
const app = express();
const port = 3000;

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

const alipaySdk = new AlipaySdk({
    appId: '9021000144671858', // 替换为你的沙箱APPID
    privateKey: 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDPUVNBKfd4xSDgC/Kled55OQYYlLOMu9DB74coRPGUttoZ9JZFlVLewIOYI4mJrJh9Fa5jrKEnK8Hx2YiDvbQoTW1alCAs4bUeMfZRuFJG0pLHWIbuZW2covSHEE+MqXKx2Mfjy/XxKo3/5CUmS5CDTFpWBc5haRcbiyegZDovUVNgOF8N8FXWg9K61DTaFmFv4UpY8vb14WuxJ6ZyVCJVAqA5lQVbOu2k7TqO2ywBYllO+Y3e3pivZlx1ejTm2og/QEXTD0PbtUqRLOQ46aHwhwN4uvVFSmrXTpEyUThDz3OLV/nmwbhWL2a/eN4CkhKY2XYzB7xvytIVS7x02i6dAgMBAAECggEASsQmhUKwR/K3x3UnnfvUet1Q0H6YPjHhuH88vA4ZHavP/jpSE2glycWd77kpOehv2fR+vt2cBLdoK2NrMy+wEDTUMi1naShBHFGiZphVu8rnOlM33jPgEvRFwYvlVHIrte3bMND2snHJYjaSGzx3MpLPpWRUqJawybMmqFZdEYDZOqFMxxNYIkQHFFXqG1FvajUUGQx75ky3eGLh1cSqYINh+9+rCbEKXfQkruyB6GNWLlOOdf6vAuWTpI0/P+eQNia7P6sNXvpweYR4LTd9fHfnv8NXV7n8Bca3LNm7wAkaNR9xWNiM59/FAuNkDCN7XuL+HK/MHoCwYX1P0t2jlQKBgQD3lrlzBFWwy5w/Kf5B0hIRPMASnhm4L31s2LWB/t6LvdksLaLwVWJoEQuvkA/qIj4rUe57zfjOeoUrKkvGT13oZrhRSx9mCcAUuJj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t6Yr2ZcdXo05tqIP0BF0w9D27VKkSzkOOmh8IcDeLr1RUpq106RMlE4Q89zi1f55sG4Vi9mv3jeApISmNl2Mwe8b8rSFUu8dNounQIDAQAB',
    // 替换为你的支付宝公钥
    gateway: 'https://openapi-sandbox.dl.alipaydev.com/gateway.do', // 沙箱环境网关
  });
  
  router.get('/createOrder', async (req, res) => {
    const outTradeNo = Date.now().toString(); // 生成订单号
    const returnUrl = `http://localhost:5173/success/${outTradeNo}`; // 动态设置 return_url
    // let qian = req.query.qian
    // const amount = qian; // 支付金额（单位：元）
    const amount = 100
    const result = await alipaySdk.pageExec('alipay.trade.wap.pay', { // 修改为移动端支付接口
      method: 'GET',
      bizContent: {
        out_trade_no: outTradeNo, // 订单号
        product_code: 'QUICK_WAP_WAY', // 修改为移动端支付产品码
        total_amount: amount, // 支付金额
        subject: '测试商品', // 商品名称
        body: '测试商品描述', // 商品描述
      },
      return_url: 'http://localhost:5173/outlet/success', // 支付成功后的回调地址
      notify_url: 'http://localhost:3000/notify', // 支付结果异步通知地址
    });
  
    res.send(result);
  });
  
  app.listen(3003, () => {
    console.log('Server is running on http://localhost:3003');
  });

  router.get('/getcant',async(req,res)=>{
    let data = await BusinessModel.find()
    console.log(data);
    
    res.send({
        code:200,
        data
    })
  })


module.exports = router;